import axios from 'axios';

const apiClient = axios.create({
  baseURL: 'http://127.0.0.1:3000/goods', // 设置所有请求的基础URL
  withCredentials: false, // 这将确保跨站点访问时不使用凭证
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
});

export default {
  // 添加货物
  addGoods(goods) {
    const formattedGoods = {
      ...goods,
      purchase_price: goods.purchasePrice,
      sale_price: goods.salePrice
    };
    delete formattedGoods.purchasePrice;
    delete formattedGoods.salePrice;
    return apiClient.post('/', formattedGoods);
  },

  // 获取所有货物
  getAllGoods() {
    return apiClient.get('/');
  },

  // 根据编码获取货物信息
  getGoodsByCode(code) {
    return apiClient.get(`/code/${code}`);
  },

  // 修改货物
  updateGoods(id, goods) {
    return apiClient.put(`/${id}`, goods);
  },

  // 删除货物
  deleteGoods(id) {
    return apiClient.delete(`/${id}`);
  },
};
